package com.bci.pwtz.controller;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.common.util.PwtzUtils;
import com.bci.pwtz.controller.admin.AdminAbstractController;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.exception.PwtzException;
import com.bci.pwtz.mysql.model.Resource;
import com.bci.pwtz.service.ResourceService;

@Controller
@RequestMapping("/resource")
public class ResourceController extends AdminAbstractController
{
    @Autowired
    private ResourceService service;

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public PaginationList<Resource> list(int pagesize, int pageno, int restype, String resName) throws PwtzException
    {
        PaginationList<Resource> list = new PaginationList<Resource>();
        try
        {
            list = service.findResource(pagesize, pageno, restype, resName);
        }
        catch (PwtzDatabaseException e)
        {
            e.printStackTrace();
        }
        return list;
    }

    @RequestMapping(value = "/load", method = RequestMethod.POST)
    @ResponseBody
    public Resource load(Long resid)
    {
        Resource res = new Resource();
        if (resid != null)
        {
            try
            {
                res = service.load(resid);
            }
            catch (PwtzDatabaseException e)
            {
                e.printStackTrace();
            }
        }
        return res;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public int add(String data, String content)
    {
        Map<String, String> map = new HashMap<String, String>();
        int result = 0;
        Resource res = new Resource();
        try
        {
            // data=java.net.URLDecoder.decode(data,"UTF-8");
            String[] list = decodeURL(data.split("&"));
            for (int i = 0; i < list.length; i++)
            {
                String[] array = list[i].split("=");
                if (array.length == 1)
                {
                    map.put(array[0], "");
                }
                else
                {
                    map.put(array[0], array[1]);
                }
            }

            res.setDescribe(map.get("describe"));
            res.setDownloadurl(map.get("downloadurl"));
            res.setPublishtime(PwtzUtils.parseDate(map.get("publishtime")));
            res.setRescontent(content);
            res.setRescontentlogo(map.get("rescontentlogo"));
            res.setReslogo(map.get("reslogo"));
            res.setSource(map.get("source"));
            res.setResname(map.get("resname"));
            res.setRestype(Integer.parseInt(map.get("restype")));
            res.setStatus(1);
            result = service.add(res);
            setLog(4, "新增资源【资源名字】【" + res.getResname() + "】成功", getUserId());
        }
        catch (PwtzDatabaseException e)
        {
            e.printStackTrace();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return result;
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public int update(String data, String content)
    {
        Map<String, String> map = new HashMap<String, String>();
        int result = 0;
        Resource res = null;
        try
        {
            // data=java.net.URLDecoder.decode(data,"UTF-8");
            String[] list = decodeURL(data.split("&"));
            for (int i = 0; i < list.length; i++)
            {
                String[] array = list[i].split("=");
                if (array.length == 1)
                {
                    map.put(array[0], "");
                }
                else
                {
                    map.put(array[0], array[1]);
                }
            }
            res = service.load(Long.valueOf(map.get("resid")));
            if (map.get("rescontentlogo") != null && !map.get("rescontentlogo").equals(res.getRescontentlogo()))
            {
                String remotePath = request.getSession().getServletContext().getRealPath("/") + "Attachment/ResourcePIC/";
                File file = new File(remotePath + res.getRescontentlogo());
                if (file.exists())
                {
                    file.delete();
                }
            }
            res.setDescribe(map.get("describe"));
            res.setDownloadurl(map.get("downloadurl"));
            res.setPublishtime(PwtzUtils.parseDate(map.get("publishtime")));
            res.setRescontent(content);
            res.setRescontentlogo(map.get("rescontentlogo"));
            res.setReslogo(map.get("reslogo"));
            res.setSource(map.get("source"));
            res.setResname(map.get("resname"));
            res.setRestype(Integer.parseInt(map.get("restype")));
            res.setStatus(1);
            result = service.update(res);
            setLog(4, "修改资源【资源名字】【" + res.getResname() + "】成功", getUserId());
        }
        catch (PwtzDatabaseException e)
        {
            e.printStackTrace();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return result;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public int delete(Long resid)
    {
        int result = 0;
        try
        {
            result = service.delete(resid);
            setLog(4, "删除资源【资源ID】【" + resid + "】成功", resid);
        }
        catch (PwtzDatabaseException e)
        {
            e.printStackTrace();
        }
        return result;

    }

}
